﻿using HuiZe.education.Model;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace HuiZe.education.View
{
    /// <summary>
    /// PART_Clound_LessionItem.xaml 的交互逻辑
    /// </summary>
    public partial class PART_Clound_LessionItem : UserControl
    {
        public RescloudrRoomItemModel Data
        {
            get { return (RescloudrRoomItemModel)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(RescloudrRoomItemModel), typeof(PART_Clound_LessionItem), new PropertyMetadata(null, (sender, e) =>
             {
                 PART_Clound_LessionItem _item = (PART_Clound_LessionItem)sender;
                 RescloudrRoomItemModel _data = null;
                 if (e.NewValue != null)
                     _data = (RescloudrRoomItemModel)e.NewValue;

                 _item.Load(_data);
             }));


        public Action<RescloudrRoomItemModel> OpenResourceCallback { get; set; }

        public PART_Clound_LessionItem()
        {
            InitializeComponent();
            _ShowStoryboard = (Storyboard)FindResource("ShowStoryboard");
        }

        public void Load(RescloudrRoomItemModel _data)
        {
            if (_data == null) return;

            BitmapImage _bmp = new BitmapImage();
            _bmp.CacheOption = BitmapCacheOption.OnLoad;
            _bmp.BeginInit();
            _bmp.UriSource = new Uri(_data.image, UriKind.RelativeOrAbsolute);
            _bmp.EndInit();
            
            PART_Image.Source = _bmp;
            PART_Title.Text = _data.name;

            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() =>
            {
                _ShowStoryboard.Begin();
            }));
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            if (OpenResourceCallback != null )
            {
                try
                {
                    OpenResourceCallback(this.Data);
                }
                catch (Exception ex)
                {
                    App.Log(ex);
                }
                e.Handled = true;
            }
            base.OnMouseLeftButtonUp(e);
        }


        Storyboard _ShowStoryboard;
    }
}
